package com.chezhujie.www.service;

import com.chezhujie.www.domain.Czjorder;
import com.chezhujie.www.domain.Czjorderdetail;
import com.chezhujie.www.domain.vo.BusinOrder;
import com.chezhujie.www.domain.vo.OrderDateVO;
import com.chezhujie.www.domain.vo.OrderIdAndDate;
import com.chezhujie.www.persistence.CzjorderMapper;
import com.xy.core.exception.ClassCheckException;
import com.xy.core.page.Pagination;
import com.xy.core.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.*;


/**
 * 订单表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class CzjorderService {
    @Autowired
    private CzjorderMapper czjorderMapper;
    @Autowired
    private CzjorderdetailService czjorderdetailService;
    @Autowired
    private CzjorderdistributionService czjorderdistributionService;

    public Pagination list(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjorderMapper.getTotalRows(params);
        List<Map<String, Object>> list = czjorderMapper.getList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }


    public void insert(Czjorder czjorder){
        if(StringUtils.isNotBlank(czjorder.getId())){
        czjorder.setId(UUIDUtils.getUUID());
        }
        czjorderMapper.insert(czjorder);
    }

    public void update(Czjorder czjorder){
        czjorder.setUpdatetime(new Date());
        czjorderMapper.update(czjorder);
    }
    public void deleteById(String id){
        czjorderMapper.deleteById(id);
    }
    public void deleteByIds(List<String> list){
        czjorderMapper.deleteByIds(list);
    }
    public Czjorder queryById(String id){
        return czjorderMapper.queryById(id);
    }
    public List<Czjorder> queryByIds(List<String> list){
        List<Czjorder> listCzjorder = czjorderMapper.queryByIds(list);
        if(listCzjorder==null){
            listCzjorder = new ArrayList<Czjorder>();
        }
        return listCzjorder;
    }
    public List<Map<String,Object>> getListUserinfoId(Map<String, Object> param) {
        return czjorderMapper.listUserinfoId(param);
    }
    public List<Map<String,Object>> getListBusinessId(Map<String, Object> param) {
        return czjorderMapper.listBusinessId(param);
    }

    public void checkEntity(Czjorder czjorder) throws Exception {
            if(czjorder.getOrderCode()!=null&&czjorder.getOrderCode().length()>25){
                throw new ClassCheckException("订单表订单编号不能超过25个字符");
            }
            if(czjorder.getRemark()!=null&&czjorder.getRemark().length()>100){
                throw new ClassCheckException("订单表备注不能超过100个字符");
            }


    }


	public Pagination listOrderDetail(Integer pageNo, Integer pageSize,
			Map<String, Object> params, ModelMap model) throws Exception{
		Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjorderMapper.getListOrderDetailRows(params);
        List<BusinOrder> list = czjorderMapper.listOrderDetail(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
	}
    public Map<String,Object> getOrderedByNo(String orderNo) {
        return czjorderMapper.getOrderedByNo(orderNo);
    }
    public int getCzjactivityCount(Map<String, Object> map) {
        return czjorderMapper.getCzjactivityCount(map);
    }

    public void updateOrderStatus(String orderno, int orderStatus) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderno", orderno);
        map.put("orderStatus", orderStatus);
        czjorderMapper.updateOrderStatus(map);
    }


	public Pagination getOrderBybusinessid(Integer pageNo, Integer pageSize,
			Map<String, Object> map, ModelMap model) {
		Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        map.putAll(pagination.toMap());
        int count = czjorderMapper.getOrderBybusinessidRows(map);
        List<BusinOrder> list = czjorderMapper.getOrderBybusinessid(map);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
	}


	public BusinOrder getOrderBybusinessidAndOrderId(Map<String, Object> map) {
		
		return czjorderMapper.getOrderBybusinessidAndOrderId(map);
	}


	public List<Integer> getPays() {
		return czjorderMapper.getPays();
	}


	public int getPay1() {
		return czjorderMapper.getPay1();
	}


	public int getPay2() {
		return czjorderMapper.getPay2();
	}

    public Map<String,Object> sumSoldOut(String id) {
        return czjorderMapper.sumSoldOut(id);
    }

    public Map<String,Object> getSalesAmount(String id) {
        return czjorderMapper.getSalesAmount(id);
    }


    public Pagination summarylist(int pageNo, int pageSize, Map<String,Object> param, ModelMap model) {
        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        param.putAll(pagination.toMap());
        int count = czjorderMapper.summarylistCount(param);
        List<Map<String,Object>> list = czjorderMapper.summarylist(param);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;

    }

    public Czjorder getOneBy(Map<String, Object> map) {
        return czjorderMapper.getOneBy(map);
    }

    public void saves(List<Czjorder> listorder) {
        if(listorder!=null&&listorder.size()>0){
            czjorderMapper.saves(listorder);
        }

    }

    public List<Map<String, Object>> getListBy(Map<String, Object> params){
        return czjorderMapper.getListby(params);
    }


	public List<OrderIdAndDate>  getOrderList(int index) {
		
		return czjorderMapper.getOrderList(index);
	}


	public void batchUpdateCzjOrder(OrderDateVO vo) {
		
		czjorderMapper.batchUpdateCzjOrder(vo);
	}


	public List<Czjorder> getOrderByOrderCode(String orderCode) {
		return czjorderMapper.getOrderByOrderCode(orderCode);
	}

    public Map<String,Object> queryDetailById(String id)  {
        return czjorderMapper.queryDetailById(id);
    }

    public void deleteAllByOrderCode(String orderNo) {
        deleteByOrderCode(orderNo);
        czjorderdetailService.deleteByOrderCode(orderNo);
        czjorderdistributionService.deleteByOrderCode(orderNo);
    }

    public void deleteByOrderCode(String orderNo) {
        czjorderMapper.deleteByOrderCode(orderNo);
    }


    public Map<String,Object> getEndLeasePrice(String orderNo) {
        try {
            Czjorderdetail orderdetail = czjorderdetailService.queryByCode(orderNo);

            Map<String, Object> moneydata = czjorderdetailService.jisuanMoney(orderdetail);
            Map<String, Object> map = new HashMap<>();
            map.put("status", 1);
            map.put("msg", "");
            map.put("code", "");
            map.put("result", moneydata);
            map.put("price", moneydata.get("money"));

            return map;
        }catch (Exception ex){
            Map<String, Object> map = new HashMap<>();
            map.put("status", 0);
            map.put("msg", "99");
            map.put("code", "程序出错");
            map.put("result", "");
            return map;
        }
    }



    public Pagination getApiList(Integer pageNo, Integer pageSize, Map<String,Object> param, ModelMap model) {
        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        param.putAll(pagination.toMap());
        int count = czjorderMapper.getApiListCount(param);
        List<Map<String,Object>> list = czjorderMapper.getApiListRow(param);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;

    }
}
